//
//  LoginViewController.m
//  Utils
//
//  Created by enghou on 2018/4/11.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "LoginViewController.h"
#import "ServerIPSettingViewController.h"
#import "LeftImageField.h"
#import "NetWorkerManager.h"
#import "NumberChecker.h"
#import "LocalPersistent.h"
#import "SelectedButton.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet LeftImageField *accountField;
@property (weak, nonatomic) IBOutlet LeftImageField *pwdField;

@property (weak, nonatomic) IBOutlet SelectedButton *rememberBtn;
@end

@implementation LoginViewController

- (IBAction)remember:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [[LocalPersistent sharedInstance]saveContent:@1 forKey:@"rememberPwd"];
    }else{
        [[LocalPersistent sharedInstance]saveContent:@0 forKey:@"rememberPwd"];
    }
}

- (IBAction)configServer:(UIButton *)sender {
    ServerIPSettingViewController *setip = [[ServerIPSettingViewController alloc]init];
    self.definesPresentationContext = YES;
    setip.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:setip animated:NO completion:nil];
}

-(void)loginSuccessAction{
    BOOL rememberPwd = [[LocalPersistent sharedInstance]contentForKey:@"rememberPwd"];
    if (rememberPwd) {
        [[LocalPersistent sharedInstance]savePwdForAccount:self.accountField.text pwd:self.pwdField.text];
    }
    [self dismissViewControllerAnimated:NO completion:nil];
    UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *tab = [mainStory instantiateInitialViewController];
    [UIApplication sharedApplication].delegate.window.rootViewController = tab;
}

- (IBAction)loginAct:(id)sender {
    if (![NumberChecker validAccount:self.accountField.text]) {
        [self alertSome:@"请输入正确的账户!"];
        return;
    }
    if (![NumberChecker validPass:self.pwdField.text]) {
        [self alertSome:@"请输入正确的密码!"];
        return;
    }
    [self beginLoading];
    [[NetWorkerManager sharedInstance]loginWithAccount:self.accountField.text password:self.pwdField.text successBlock:^(BaseModel *model) {
        [self endLoading];
        if (model.success) {
            //根据是否记住密码记住密码，跳到主界面
            [self loginSuccessAction];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}


-(void)configUI{
    UIImage *image = [UIImage imageNamed:@"icon_bg"];
    [self.view performSelector:@selector(setImage:) withObject:image];
    BOOL remember = [[[LocalPersistent sharedInstance]contentForKey:@"rememberPwd"]boolValue];
    _rememberBtn.selected = remember;
    if (remember) {
        NSString *account;
        NSString *pwd;
        [[LocalPersistent sharedInstance]latestLoginUser:&account passWord:&pwd];
        self.accountField.text = account;
        self.pwdField.text = pwd;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
